package com.zhuoyue.biz.core.command.cmd.common.faa.base;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.common.constant.ControlType;
import com.zhuoyue.common.constant.DataBase;
import com.zhuoyue.common.utils.CmdUtils;

/**
 * @author Administrator
 */
public class BaseCmd extends ControlCmd {
    private static String wayStr = "1";

    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq, String way) {
        wayStr = way;
        String cmd = "";
        switch (controlReq.getControlType()) {
            case ControlType.TURN_ON_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, open());
                break;
            case ControlType.TURN_OFF_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, close());
                break;
            case ControlType.PAUSE_REQUEST:
                cmd = getControlCmd(deviceAndGatewayDO, pause());
                break;
            case ControlType.TIMING_PAUSE:
                int delayTime = controlReq.getActions().get("delayTime") == null ? 0 : (int) controlReq.getActions().get("delayTime");
                cmd = getControlCmd(deviceAndGatewayDO, pauseConfirmation(CmdUtils.getHexString(delayTime)));
                break;
            case ControlType.TIMING_TURN_ON_REQUEST:
                int delayTime1 = controlReq.getActions().get("delayTime") == null ? 0 : (int) controlReq.getActions().get("delayTime");
                cmd = getControlCmd(deviceAndGatewayDO, openConfirmation(CmdUtils.getHexString(delayTime1)));
                break;
            case ControlType.TIMING_TURN_OFF_REQUEST:
                int delayTime2 = controlReq.getActions().get("delayTime") == null ? 0 : (int) controlReq.getActions().get("delayTime");
                cmd = getControlCmd(deviceAndGatewayDO, closeConfirmation(CmdUtils.getHexString(delayTime2)));
                break;
            default:
                break;
        }
        return cmd;
    }

    public static String open() {
        return IndexWay.getEnumType(wayStr) + DataBase.DIRECT + "01";
    }

    public static String close() {
        return IndexWay.getEnumType(wayStr) + DataBase.DIRECT + "02";
    }

    public static String pause() {
        return IndexWay.getEnumType(wayStr) + DataBase.DIRECT + "04";
    }


    public static String openConfirmation(String time) {
        return IndexWay.getEnumType(wayStr + "d") + DataBase.DELAYED + "01" + time;
    }

    public static String closeConfirmation(String time) {
        return IndexWay.getEnumType(wayStr + "d") + DataBase.DELAYED + "02" + time;
    }

    public static String pauseConfirmation(String time) {
        return IndexWay.getEnumType(wayStr + "d") + DataBase.DELAYED + "04" + time;
    }

}
